package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品接口")
    public Result saveDish(@RequestBody DishDTO dishDTO){
        dishService.saveDishAndFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询接口")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除菜品功能
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除菜品接口")
    public Result delByIds(@RequestParam List<Long> ids){
        dishService.delByIds(ids);
        //删除菜品之后要把redis中对应的数据删除
        ids.forEach(id ->{
            DishVO dishVO = dishService.findById(id);
            String CACHE_DISH_KEY = "DISH_" + dishVO.getCategoryId();
            redisTemplate.delete(CACHE_DISH_KEY);
        });
        return Result.success();
    }

    /**
     * 根据id查询菜品功能
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品接口")
    public Result<DishVO> findById(@PathVariable Long id){
        DishVO dishVO = dishService.findById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品信息功能
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品接口")
    public Result update(@RequestBody DishDTO dishDTO){
        dishService.update(dishDTO);
        //如果修改成功,需要把redis里面对应的该菜品的分类数据删除
        String CACHE_DISH_KEY = "DISH_" + dishDTO.getCategoryId();
        redisTemplate.delete(CACHE_DISH_KEY);
        return Result.success();
    }

    /**
     * 启售或停售菜品功能
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启售或停售菜品接口")
    public Result enableOrDisable(@PathVariable Integer status,Long id){
        dishService.enableOrDisable(status,id);
        //如果修改成功,要把redis中对应的数据删除
        DishVO dishVO = dishService.findById(id);
        String CACHE_DISH_KEY = "DISH_" + dishVO.getCategoryId();
        redisTemplate.delete(CACHE_DISH_KEY);
        return Result.success();
    }

    /**
     * 根据分类id或名字查询菜品功能
     * @param dishDTO
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询或名字菜品接口")
    public Result findByCategoryId(DishDTO dishDTO){
        List<DishVO> list = dishService.findByConditions(dishDTO);
        return Result.success(list);
    }
}
